package main

func numIslands(grid [][]byte) int {
	result := 0
	for i, row := range grid {
		for j, v := range row {
			if v == '1' {
				result++
				bfs(grid, i, j)
			}
		}
	}
	return result
}

func bfs(grid [][]byte, i, j int) {
	if i < 0 || j < 0 || i >= len(grid) || j >= len(grid[0]) {
		return
	}
	if grid[i][j] == '0' {
		return
	}
	grid[i][j] = '0'
	bfs(grid, i-1, j)
	bfs(grid, i+1, j)
	bfs(grid, i, j-1)
	bfs(grid, i, j+1)
}

func main() {
	grid := [][]byte{
		{'1', '1', '1', '1', '0'},
		{'1', '1', '0', '1', '0'},
		{'1', '1', '0', '0', '0'},
		{'0', '0', '0', '0', '0'},
	}
	println(numIslands(grid))

}
